﻿myApp.controller('FBSignUpController', function ($scope,$location,authService,$timeout,$facebook,$http,$rootScope) {
    document.title = "Penang Realty 世界地產 Member Registration";
    $scope.$emit('newPageLoaded', { 'title': 'Penang Realty 世界地產 Member Registration', 'description': 'Register to become our member to enjoy extra benefits.' });

    $scope.data = {};
    $facebook.api("/me?fields=email,name").then(function (response) {
        console.log("PROFILE DETAIL: " + JSON.stringify(response));
        $scope.data.Name = response.name;
        $scope.data.Email = response.email;
        $scope.data.FbId = response.id;
        $scope.data.Password = "";
    });

    $scope.close = function () {
        $scope.warning = null;
        $scope.error = null;
        $scope.info = null;
    }
    var startTimer = function () {
        $timeout(function () {
            $rootScope.isAuth = true;

            $location.path('/')
            setTimeout(function () {
                $window.location.reload();
            }, 1000);
        }, 2000);
    }

    function validateEmail(email) {
        var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        return re.test(email);
    }





    $scope.save = function () {
        console.log("Saving...");
        if (!validation()) return;

        console.log(JSON.stringify($scope.data));

        $http.post('/api/User/saveFb', $scope.data, {
            headers:
            { 'Content-Type': 'application/json;charset=UTF-8' }
        }).success(function (response) {
            console.log("SUCCESS!: " + JSON.stringify(response));
            $scope.info = "Registration successful! You will be redirected to home page in 2 secs";
            startTimer();

        }).error(function (error) {
            alert("ERROR: " + JSON.stringify(error));
        });
     
        //authService.saveRegistration($scope.data).then(function (response) {
        //    if (response.data == "Success") {
        //        $scope.info = "Registration successful! You will be redirected to login page in 2 secs";
        //        console.log(response);
        //        startTimer();
        //    }
        //    else
        //        $scope.info = response.data;
        //}),
        //function (response) {

        //    console.log(response);
        //};


        // $http.post('api/User/save', $scope.data )
        //.success(function (result) {
        //    var output = result;
        //    if (output == "Success") {

        //    }

        //})

    }

   




    function validation() {
        $scope.warning = [];
        if (!$scope.data.Name)
            $scope.warning.push({ msg: "Name cannot be empty!" });
        if (!$scope.data.Email)
            $scope.warning.push({ msg: "Email must not be empty!" });
        if (!$scope.data.Handphone)
            $scope.warning.push({ msg: "Handphone must not be empty!" });
        else {
            $scope.warning = null;
            return true;
        }
    }
});